package com.deliinsns.sys;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.deliinsns.sys.util.SystemConstant;
import net.sf.json.JSONObject;
public class BasicController  {
	protected JSONObject json= new JSONObject();
	protected Messenger msg= new Messenger();
	
	public  void out(HttpServletRequest request, HttpServletResponse response,MsgType type ) {
		response.setCharacterEncoding(SystemConstant.SYSCHARACTER);//设置系统编码
		Properties ppt= getCodeDesc(request);
		try {
			if(type== MsgType.msg){
					msg.setMsg(ppt.getProperty(msg.getMsgCode()));
					PrintWriter writer= response.getWriter();
					writer.write(msg.toString());
					writer.flush();
			}else if(type== MsgType.json){
					PrintWriter writer= response.getWriter();
					writer.write(json.toString());
					writer.flush();
			}
			reset();
		} catch (IOException e) {
			reset(); 
			e.printStackTrace();
		}
	}
	public boolean checkParam(String ...params){
		 for(String p : params){
			 if(StringUtils.isEmpty(p))
				 return false;
		 }  
		 return true;
	}
	public Properties getCodeDesc(HttpServletRequest request) {
		ServletContext servletContext= request.getSession().getServletContext();
		return (Properties) servletContext.getAttribute("codeDesc");
	}
	private void reset(){
		json= new JSONObject();
		msg= new Messenger();
	}
}

